package com.lanling.educenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanling.educenter.entity.UcenterMember;
import com.lanling.educenter.entity.vo.RegisterVo;
import com.lanling.educenter.entity.vo.StudentQuery;
import com.lanling.educenter.other.R;
import com.lanling.educenter.other.UcenterMemberOrder;
import com.lanling.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 胡振华
 * @since 2020-11-21
 */
@RestController
@RequestMapping("/educenter/ucenter-member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        Map map = memberService.login(member);
        return R.ok().data(map);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }

    //查询所有用户
    @ApiOperation(value = "多条件组合分页查询")
    @PostMapping("getAllStudents")
    public R getAllStudents(@RequestBody(required = false) StudentQuery studentQuery){
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        String mobile = studentQuery.getMobile();
        String nickname = studentQuery.getNickname();
        String sex = studentQuery.getSex();
        String isDisabled = studentQuery.getIsDisabled();
        String begin = studentQuery.getBegin();
        String end = studentQuery.getEnd();
        if (!StringUtils.isEmpty(mobile)){
            queryWrapper.like("mobile",mobile);
        }
        if (!StringUtils.isEmpty(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        if (!StringUtils.isEmpty(sex)){
            queryWrapper.eq("sex",sex);
        }
        if (!StringUtils.isEmpty(isDisabled)) {
            queryWrapper.eq("is_disabled",isDisabled);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }
        queryWrapper.orderByDesc("gmt_create");
        List<UcenterMember> ucenterMembers = memberService.list(queryWrapper);
        return R.ok().data("records",ucenterMembers);
    }

    //根据Id删除用户
    @GetMapping("removeStudent/{id}")
    public R removeStudent(@PathVariable String id){
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setId(id);
        ucenterMember.setIsDeleted(1);
        memberService.updateById(ucenterMember);
        return R.ok();
    }

    //根据Id禁用用户
    @GetMapping("disabledStudent/{id}/{type}")
    public R disabledStudent(@PathVariable String id,@PathVariable String type){
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setId(id);
        ucenterMember.setIsDisabled(Integer.parseInt(type));
        memberService.updateById(ucenterMember);
        return R.ok();
    }

    //根据id查询某个用户的信息
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        UcenterMember ucenterMember = memberService.getUserInfo(request);
        if (ucenterMember == null){
            return R.error().data("isSuccess",false);
        }else{
            return R.ok().data("isSuccess",true).data("user",ucenterMember);
        }
    }

    //注册情况，性别分布情况，年龄分布情况
    @GetMapping("userAnalyse")
    public R userAnalyse(){
        Map<String,Map<String,Integer>> map = memberService.userAnalyse();
        return null;
    }

}

